module("luci.controller.hengsion.hsion_mLink", package.seeall)

function index()
	entry({"admin", "network", "hsion_mLink"},
		alias("admin", "network", "hsion_mLink", "setting"),
		_("MultiLink"), 80).i18n = "hsion_mLink.zh-cn"
		
	entry({"admin","network", "hsion_mLink", "setting"},
			cbi("hengsion/hsion_mLink2"),
			_("Setting"), 1).leaf = true
	entry({"admin", "network", "hsion_mLink", "status"}, 
		template("hengsion/hsion_mLink_status"), _("Status"), 2).leaf = true
	entry({"admin", "network", "hsion_mLink", "hsion_mLink_status_refresh"}, call("ajax_hsion_mLink_status_refresh"))	
end

function ajax_hsion_mLink_status_refresh()
	local refresh = luci.http.formvalue("hsion_mLink_status_refresh")
	
	require("uci")
	x = uci.cursor()
	hsion_mLink_iface = {}
	hsion_mLink_status = {}
	count = 0
	x:foreach("hsion_mLink", "interface", function(s)
		local iface = ""
		local status = ""
		for opt, val in pairs(s) do
			--print('\t' .. opt .. ':' .. tostring(val))
			if opt == "net" then
				iface = tostring(val)
			end
			if opt == "track_status" then
				status = tostring(val)
			end
		end
		count = count + 1
		table.insert(hsion_mLink_iface, count, iface)
		table.insert(hsion_mLink_status, count, status)
	end)
	
	local pid = 0
	local fd = io.popen("pidof hsion_mLink");
	if fd then
		pid = fd:read("*a")
		fd:close()
	end
	luci.http.prepare_content("application/json")
	luci.http.write_json({ pid=pid, count=count, hsion_mLink_iface=hsion_mLink_iface, hsion_mLink_status=hsion_mLink_status})
end
